package com.bruce.main.repository;

import com.bruce.main.MainApplication;
import com.bruce.main.bean.SignBean;
import com.j256.ormlite.stmt.QueryBuilder;

import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import io.reactivex.Observable;
import io.reactivex.ObservableEmitter;
import io.reactivex.ObservableOnSubscribe;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.annotations.NonNull;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;
import io.reactivex.schedulers.Schedulers;
import me.goldze.mvvmhabit.lks.dbmanager.LksSqliteOpenHelper;
import me.goldze.mvvmhabit.lks.dbmanager.OrmDaoUtils;
import me.goldze.mvvmhabit.lks.dbmanager.WhereInfo;
import me.goldze.mvvmhabit.utils.TimeUtil;

public class SingRepository {
    OrmDaoUtils<SignBean> customDao;
    private static volatile SingRepository instance = null;

    public SingRepository() {
        try {
            customDao = LksSqliteOpenHelper.getHelper(MainApplication.application).getCustomDao(MainApplication.application, SignBean.class);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
    }

    public static SingRepository getInstance() {
        if (instance == null) {
            synchronized (SingRepository.class) {
                if (instance == null) {
                    instance = new SingRepository();
                }
            }
        }
        return instance;
    }
    public void insertSign(Consumer<? super Disposable> onSubscribe,long time,Observer observer) {

        Observable<SignBean> observable = Observable.create(new ObservableOnSubscribe<SignBean>() {
            @Override
            public void subscribe(@NonNull ObservableEmitter<SignBean> emitter) throws Exception {
                try {
                    SignBean signBean = new SignBean();
                    signBean.setTime(time);
                    signBean.setDayStr(TimeUtil.getTimeFromMillisecond(time));
                    int insert = customDao.insert(signBean);
                    emitter.onNext(signBean);
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                    emitter.onError(throwables);
                }
            }
        });
        setSubscribe(onSubscribe, observable, observer);
    }
    public void deleteById(Consumer<? super Disposable> onSubscribe,int id,Observer observer) {

        Observable<Boolean> observable = Observable.create(new ObservableOnSubscribe<Boolean>() {
            @Override
            public void subscribe(@NonNull ObservableEmitter<Boolean> emitter) throws Exception {
                try {
                    customDao.deleteById(id);
                    emitter.onNext(true);
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                    emitter.onError(throwables);
                }
            }
        });
        setSubscribe(onSubscribe, observable, observer);
    }
    public void queryByDay(Consumer<? super Disposable> onSubscribe,String dayStr,int page,int pageSize,Observer observer) {

        Observable<List<SignBean>> observable = Observable.create(new ObservableOnSubscribe<List<SignBean>>() {
            @Override
            public void subscribe(@NonNull ObservableEmitter<List<SignBean>> emitter) throws Exception {
                try {
                    WhereInfo whereInfo=WhereInfo.get();
                    whereInfo.currentPage=page;
                    whereInfo.limit=pageSize;
                    whereInfo.size=pageSize;
                    whereInfo.equal("dayStr",dayStr);
                    List<SignBean> signBeans = customDao.queryLimit(whereInfo);


                    emitter.onNext(signBeans);
                } catch (Exception throwables) {
                    throwables.printStackTrace();
                    emitter.onError(throwables);
                }
            }
        });
        setSubscribe(onSubscribe, observable, observer);
    }
    public void queryCount(Consumer<? super Disposable> onSubscribe, Observer observer) {

        Observable<Long> observable = Observable.create(new ObservableOnSubscribe<Long>() {
            @Override
            public void subscribe(@NonNull ObservableEmitter<Long> emitter) throws Exception {
                try {
                    long count = customDao.queryBuilder().countOf();

                    emitter.onNext(count);

                } catch (Exception throwables) {
                    throwables.printStackTrace();
                    emitter.onError(throwables);
                }
            }
        });
        setSubscribe(onSubscribe, observable, observer);
    }
    public void queryCountDay(Consumer<? super Disposable> onSubscribe,Observer observer) {

        Observable<Long> observable = Observable.create(new ObservableOnSubscribe<Long>() {
            @Override
            public void subscribe(@NonNull ObservableEmitter<Long> emitter) throws Exception {
                try {
                    List<SignBean> signBeans = customDao.queryBuilder().groupBy("dayStr").query();
                    emitter.onNext((long) signBeans.size());

                } catch (Exception throwables) {
                    throwables.printStackTrace();
                    emitter.onError(throwables);
                }
            }
        });
        setSubscribe(onSubscribe, observable, observer);
    }
    public void queryGroupByDay(Consumer<? super Disposable> onSubscribe,Observer observer) {

        Observable<List<SignBean>> observable = Observable.create(new ObservableOnSubscribe<List<SignBean>>() {
            @Override
            public void subscribe(@NonNull ObservableEmitter<List<SignBean>> emitter) throws Exception {
                try {
                    List<SignBean> signBeans = customDao.queryBuilder().groupBy("dayStr").query();
                    emitter.onNext(signBeans);

                } catch (Exception throwables) {
                    throwables.printStackTrace();
                    emitter.onError(throwables);
                }
            }
        });
        setSubscribe(onSubscribe, observable, observer);
    }
    private <T> void setSubscribe(Consumer<? super Disposable> onSubscribe, Observable<T> observable, Observer observer) {
        observable.subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())//回调到主线程
                .doOnSubscribe(onSubscribe)
                .subscribe(observer);
    }
}
